class CosHandler {
    constructor(cos) {
        this.cos = cos;
        this.bucket = 'sky-1319850798';
        this.region = 'ap-nanjing';
    }

    // 获取对象
    _getObject(key) {
        return new Promise((resolve, reject) => {
            this.cos.getObject({
                Bucket: this.bucket,
                Region: this.region,
                Key: key
            }, function(err, data) {
                if (err) {
                    reject(err);
                } else {
                    resolve(data);
                }
            });
        });
    }

    // 获取历史记录
    async getHistory() {
        try {
            const response = await this._getObject('query_history.json');
            if (response && response.Body) {
                return JSON.parse(response.Body);
            }
            return [];
        } catch (error) {
            console.error('获取历史记录失败:', error);
            return [];
        }
    }

    // 获取公告
    async getAnnouncement() {
        try {
            const response = await this._getObject('announcement.json');
            if (response && response.Body) {
                const data = JSON.parse(response.Body);
                return data.announcement;
            }
            return null;
        } catch (error) {
            console.error('获取公告失败:', error);
            return null;
        }
    }

    // 获取用户数据
    async getUserData() {
        try {
            const response = await this._getObject('用户.json');
            if (response && response.Body) {
                return JSON.parse(response.Body);
            }
            return null;
        } catch (error) {
            console.error('获取用户数据失败:', error);
            return null;
        }
    }

    // 获取装扮类型名称
    static getAdornTypeName(key) {
        const typeNames = {
            wing: '斗篷',
            prop: '背饰',
            neck: '项链',
            mask: '面具',
            horn: '头饰',
            hat: '发饰',
            hair: '发型',
            feet: '鞋子',
            face: '面饰',
            body: '裤子',
            pants: '裤子',
            cloak: '斗篷'
        };
        return typeNames[key] || key;
    }

    static formatUserId(userId) {
        if (!userId) return '-';
        if (userId.length > 12) {
            return userId.slice(0, 6) + '...' + userId.slice(-6);
        }
        return userId;
    }

    static formatTimestamp(timestamp) {
        const date = new Date(timestamp);
        const now = new Date();
        const diff = now - date;

        // 如果是今天的记录，只显示时间
        if (date.toDateString() === now.toDateString()) {
            return date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' });
        }

        // 如果是最近7天的记录，显示"x天前"
        if (diff < 7 * 24 * 60 * 60 * 1000) {
            const days = Math.floor(diff / (24 * 60 * 60 * 1000));
            return days === 0 ? '今天' : `${days}天前`;
        }

        // 其他情况显示完整日期
        return date.toLocaleString('zh-CN', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit'
        });
    }
}

// 初始化 COS 配置
const cos = new COS({
    SecretId: 'AKID590d4KNEvQvY6Zu43qbBamkX9anmY9Xi',
    SecretKey: '2qQn4G8yjJhtttshWTGzdEvbFm8NSPDp'
});

const Bucket = 'sky-1319850798';
const Region = 'ap-nanjing';

// 创建全局实例
window.cosHandler = new CosHandler(cos);